package anc.tankshooter.models;

import java.util.ArrayList;

import org.andengine.opengl.texture.region.ITiledTextureRegion;

import anc.tankshooter.GameSetting;
import anc.tankshooter.map.GameMap;

public class Bullet extends IDymanicObject {

	public enum BulletType {FIRE,LASER};
	
	private BulletType mType;
	private ITank mShooter;
	
	public Bullet(float pX, float pY, ITiledTextureRegion pTiledTextureRegion) 
	{
		super(pX, pY, pTiledTextureRegion);
		setZIndex(2);
		mShooter = null;
		setSpeed(GameSetting.SPEED_BULLET);
		mDurations = new long[]{100, 100, 100, 100}; 
		mFrames = new int[4];
		mLoopCounter = -1;
		setType(BulletType.FIRE);
	}

	public BulletType getType() {
		return mType;
	}

	public void setType(BulletType mType) {
		this.mType = mType;
		switch(mType){
		case FIRE:
			for(int i=0;i<mFrames.length;i++){
				mFrames[i]=0+i;
			}
			break;
		case LASER:
			setSpeed(GameSetting.SPEED_BULLET+1);
			for(int i=0;i<mFrames.length;i++){
				mFrames[i]=4+i;
			}
			break;
		}
		this.start();
	}

	public ITank getShooter(){
		return mShooter;
	}
	public void setShooter(ITank tank){
		mShooter = tank;
	}
	@Override
	public void setDir(MovingDirection dir){
		
		this.mDir=dir;
		switch(mDir){
		case UP:
			this.setRotation(180);
			this.mMoveVector = new float[] {0, 0 - this.mMoveSpeed};
			break;
		case RIGHT:
			this.mMoveVector = new float[] {0 + this.mMoveSpeed, 0};
			this.setRotation(-90);
			break;
		case DOWN:
			this.mMoveVector = new float[] {0, 0 + this.mMoveSpeed};
			this.setRotation(0);
			break;
		case LEFT:
			this.mMoveVector = new float[] {0 - this.mMoveSpeed, 0};
			this.setRotation(90);
			break;
		}
	}
	@Override
	public void onUpdate(GameMap map,float elapseSecond) 
	{
		if(!mIsDie){
			move(elapseSecond);
			if (isTouchEdge()) {
				mIsDie = true;
			}
		}
	}

	@Override
	public void reset() {
		
	}
	
	/**
	 * Kiem tra co va cham tren map hay khong
	 */
	public boolean isCollisionWithMap(GameMap gmap)
	{
		boolean isCollision = false;
		int px = (int) (getCenterX() / GameMap.TITLE_WIDTH);
		int py = (int) (getCenterY() / GameMap.TITLE_HEIGHT);
		/*switch (mDir) 
		{
			case UP:
				py--;
				break;
			case RIGHT:
				px++;
				break;
			case DOWN:
				py++;
				break;
			case LEFT:
				px--;
				break;
		}*/
		if(px >= 0 && px < GameMap.MAP_COLUMNS & py >= 0 && py < GameMap.MAP_ROWS)
		{
			Rect tileBound = new Rect(px * GameMap.TITLE_WIDTH, 
									(px + 1) * GameMap.TITLE_WIDTH, 
									py * GameMap.TITLE_HEIGHT, 
									(py + 1) * GameMap.TITLE_HEIGHT);
			if(getBound().collidesWith(tileBound)) {
				isCollision = gmap.isObstacle(gmap.getUnbreakableObstacle().getTMXTiles()[py][px].getGlobalTileID());
			}	
		}
		return isCollision;
	}
	
	public boolean isCollisionWithITank(ArrayList<ITank> itanks){
		boolean isCollise = false;
		for (int i = 0; !isCollise &&  i < itanks.size(); i++) {
			ITank tank = itanks.get(i);
			if(mShooter instanceof Enermy && tank instanceof Enermy )
				continue;
			if( !tank.isBlind()&& this.collidesWith(itanks.get(i)) && mShooter != tank){
				isCollise = true;
				tank.setDead(true);
			}
		}
		
		return isCollise;
	}
	/**
	 * Kiem tra co gay no tren map hay khong. 
	 * Tra ve toa do gay no. Tra ve [-1][-1] neu khong gay no
	 * @param gmap
	 * @return
	 */
	public boolean isBreakOnMap(GameMap gmap)
	{
		boolean isBreak = false;
		int px = (int) (getCenterX() / GameMap.TITLE_WIDTH);
		int py = (int) (getCenterY() / GameMap.TITLE_HEIGHT);
		/*switch (mDir) 
		{
			case UP:
				py--;
				break;
			case RIGHT:
				px++;
				break;
			case DOWN:
				py++;
				break;
			case LEFT:
				px--;
				break;
		}*/
		if(px >= 0 && px < GameMap.MAP_COLUMNS & py >= 0 && py < GameMap.MAP_ROWS)
		{
			Rect tileBound = new Rect(px * GameMap.TITLE_WIDTH, 
					(px + 1) * GameMap.TITLE_WIDTH,
					py * GameMap.TITLE_HEIGHT, 
					(py + 1) * GameMap.TITLE_HEIGHT);
			if(getBound().collidesWith(tileBound))
			{
				if(gmap.isObstacle(gmap.getBreakableObstacle().getTMXTiles()[py][px].getGlobalTileID()))
				{
					gmap.destroyTile(px, py);
					this.setPosition(px * GameMap.TITLE_WIDTH, py * GameMap.TITLE_HEIGHT);
					isBreak = true;
				}
			}	
		}
		return isBreak;
	}
	
	@Override
	public Rect getBound() 
	{
		mRectBound.mLeft=getX();
		mRectBound.mTop=getY();
		mRectBound.mRight=mRectBound.mLeft+getWidth();
		mRectBound.mBottom=mRectBound.mTop+getHeight();
		
		switch(mDir){
		case UP:
			mRectBound.mLeft = getX() + 7;
			mRectBound.mTop = getY() - 3;
			mRectBound.mRight = mRectBound.mLeft + 16;
			mRectBound.mBottom = mRectBound.mTop + 24;
			break;
		case RIGHT:
			mRectBound.mLeft = getX() + 3;
			mRectBound.mTop = getY() + 7;
			mRectBound.mRight = mRectBound.mLeft + 24;
			mRectBound.mBottom = mRectBound.mTop + 16;
			break;
		case DOWN:
			mRectBound.mLeft = getX() + 7;
			mRectBound.mTop = getY() - 3;
			mRectBound.mRight = mRectBound.mLeft + 16;
			mRectBound.mBottom = mRectBound.mTop + 24;
			break;
		case LEFT:
			mRectBound.mLeft = getX() + 3;
			mRectBound.mTop = getY() + 7;
			mRectBound.mRight = mRectBound.mLeft + 24;
			mRectBound.mBottom = mRectBound.mTop + 16;
			break;
		}
		return mRectBound;
	}
}
